"""
__author__ = Zdenek Maxa

TODO:
locally fails due to impossibility to run grid authentication chain ...

HAVE NOT BEEN UPDATED FOR A WHILE ...

py.test unittest testing the whole transfer chain locally
this one expects failure as FDT server can't write into destination directory

run 'py.test transfer_fails.py -s' which doesn't capture stdout
more notes on py.test + multiprocession in transfer.py

test is supposed to fail, simmulates running 
fdtcp.py -p 8000 -P 8001 localhost:/tmp/file1 localhost:/aaa/bbbb/file2
-> can't write into destination
"""


import os
import sys
import tempfile
import logging
    
import time
import signal
import filecmp

import py.test
from multiprocessing import Process
from mock import Mock


# tests are in a separate directory, need to make project directory available
baseDir = os.path.abspath(__file__).rsplit(os.sep, 2)[0]
sys.path.append(baseDir)


from fdtcp import TransferFile
from fdtcp import Transfer
from fdtcp import Transfers
from fdtcp import ConfigFDTCopy
from fdtd import ConfigFDTD
from fdtd import FDTD
from common.errors import FDTDException
from common.errors import FDTCopyException
from common.errors import ServiceShutdownBySignal
from utils.Logger import Logger



def setup_module():
    print "this is setup"


def teardown_module():
    global baseDir
    print "this is teardown"
    sys.path.remove(baseDir)
    
        
    
def testWholeTransferChain():    
    logger = Logger("test logger", level = logging.DEBUG)    
    # create FDTD services running locally
    processes = []
    transfers = None
    try:
        inputOptions = "-d DEBUG -p 8000"
        conf = ConfigFDTD(inputOptions.split())
        conf.sanitize()     
        daemon = FDTD(conf, logger)
        p = Process(target = daemon.start)
        p.start()
        processes.append((p, daemon))
    
        inputOptions = "-d DEBUG -p 8001"
        conf = ConfigFDTD(inputOptions.split())
        conf.sanitize()
        daemon = FDTD(conf, logger)
        p = Process(target = daemon.start)
        p.start()
        processes.append((p, daemon))

        fileContent = "my data"
        f = open("/tmp/myfile1", 'w')
        f.write(fileContent)
        f.close()

        # run fdtdcp now
        conf = ConfigFDTCopy("-p 8000 -P 8001 localhost:/tmp/file1 localhost:/aaa/bbbb/file2".split())
        conf.sanitize()
        transfers = Transfers(conf, logger)
        t = transfers.transfers["localhostlocalhost"]
        assert len(t.toCleanup) == 0
        
        # actual transfer (tested that finally clause is run fine and gracefully if this fails)
        t.performTransfer()
                        
    finally:
        # test if the objects exists - might not have been created
        if transfers:
            for t in transfers.transfers.values():
                t.performCleanup()
                
                # client side ...
                # check if the actual FDTCopy attribute exists - might have failed while creating it
                if t.sender:
                    t.sender.proxy._release()
                if t.receiver:
                    t.receiver.proxy._release()
        
        # after clean up actions - stop daemons / services
        for p, daemon in processes:
            daemon.shutdown() # shutdown the service first
            print "send the FDTD signal to terminate ... will raise ServiceShutdownBySignal ..."
            p.terminate() # will send SIGTERM (rather than doing os.kill(p.pid, signal.SIGTERM) 
            print "running process FDTD (join) ..."
            p.join() # terminated the wrapping process in this test